package org.alex.farouq.exam.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.JFrame;

import org.alex.farouq.exam.model.question.SubQuestion;
import org.alex.farouq.exam.view.*;
import org.hamcrest.core.IsInstanceOf;

/**
 * 
 * @author Administrator
 */
public class Controller implements ActionListener {
	private MainWindow mainWindow = null;
	private QuestionWindow questionWindow = null;
	private SubQuestionWindow subQuestionWindow = null;
	private static Controller controller = null;

	private Controller() {

	}

	public static Controller getInstance() {
		if (controller == null) {
			controller = new Controller();
		}
		return controller;
	}

	public void addObserver(JFrame window) {
		if (window instanceof MainWindow) {
			mainWindow = (MainWindow) window;
		} else if (window instanceof QuestionWindow) {
			questionWindow = (QuestionWindow) window;
		} else if (window instanceof SubQuestionWindow) {
			subQuestionWindow = (SubQuestionWindow) window;
		}
	}

	
	@Override
	public void actionPerformed(ActionEvent e) {
		String buttonName = ((JButton) (e.getSource())).getName();
		switch (Integer.parseInt(buttonName)) {
		// ButtonClicked.addQuestionButtonClicked
		case 1:
			mainWindow.addNewQuestionButtonActionPerformed();
			break;
		// ButtonClicked.previewQuestionButtonClicked
		case 2:
			// TODO replace this method with a suitable one
			if (mainWindow.previewSelectedQuestionButtonActionPerformed() != -1) {
				// TODO retreive its data from model
				// pass this data to a new created question window
				String[] subQuestions = {"sub1", "sub2", "sub3" };
				mainWindow.setQuestionToBeEdited("ques", 2, subQuestions);
				// TODO get the header text from model and put it as param in
				// next function
				mainWindow.addEditQuestionItemActionPerformed("newQues");
			}
			break;
		// ButtonClicked.removeQuestionButtonClicked
		case 3:
			int index = mainWindow.removeSelectedQuestion();
			if (index > -1) {
				// TODO remove the question from model
			}
			break;
		// ButtonClicked.removeSubQuestionButtonClicked
		case 4:
			int[] path = mainWindow.removeSelectedSubQuestion();
			if (path != null) {
				// TODO remove the subquestion from model
			}
			break;
			//generate exam button
		case 5:
			//TODO generate exam actions view and model
		// ButtonClicked.addSubQuestionButtonClicked
		case 11:
			questionWindow.addSubQuestionActionPerformed();
			break;
		// ButtonClicked.editSubQuestionButtonClicked
		case 12:
			if (questionWindow.editButtonActionPerformed() != -1) {
				// TODO retreive its data from model
				// pass this data to a new created subquestion window
				String[] answers = { "right", "wrong1", "wrong2", "wrong3" };
				questionWindow.setSubQuestionToBeEdited("sub", 2, "10", "15",
						answers);
				// TODO get the header text from model and put it as param in
				// next function
				questionWindow.addEditSubQuestionItemActionPerformed("newsub");
			}
			break;
		// ButtonClicked.deleteSubQuestionButtonClicked
		case 13:
			if (questionWindow.deleteButtonActionPerformed() != -1) {
				// TODO delete the subquestion from model
			}
			break;
		// ButtonClicked.saveQuestion
		case 14:
			if (questionWindow.saveQuestionButtonActionPerformed()) {
				// TODO save question in model
				mainWindow.addNewQuestion(questionWindow.getHeaderText());
				// TODO calculate totalmarks and duration
				mainWindow.upateTotalExamMarkAndDurationLabels(150, 90);
			}

			break;
		// ButtonClicked.cancelButtonClicked
		case 15:
			questionWindow.cancelCloseButtonActionPerformed();
			break;
		// ButtonClicked.addChoiceButtonClicked
		case 21:
			if (subQuestionWindow.addChoiceButtonActionPerformed()) {
				if (subQuestionWindow.isCorrectChoice()) {
					// TODO delete the correct choice from model
				} else {
					// TODO delete the wrong choice from model
				}
			}
			break;
		// ButtonClicked.editChoiceButtonClicked
		case 22:
			subQuestionWindow.editChoiceButtonActionPerformed();
			break;
		// ButtonClicked.deleteChoiceButtonClicked
		case 23:
			if (subQuestionWindow.deleteChoiceButtonActionPerformed() != -1) {
				if (subQuestionWindow.isCorrectChoice()) {
					// TODO delete the correct choice from model
				} else {
					// TODO delete the wrong choice from model
				}
			}
			break;
		// ButtonClicked.doneButtonClicked
		case 24:
			if (subQuestionWindow.doneButtonActionPerformed()) {
				// TODO save sub question in model
				// TODO put in the next parameter the header.text of the saved
				// subquestion
				questionWindow
						.addEditSubQuestionItemActionPerformed("new added sub");
			}

			break;
		// ButtonClicked.cancelButtonClicked
		case 25:
			subQuestionWindow.cancelButtonActionPerformed();
			break;
		}
	}
}
